package com.zack.gank.gankandroid.sexy.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.provider.SyncStateContract;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.graphics.Palette;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.zack.gank.gankandroid.gankandroid.R;
import com.zack.gank.gankandroid.main.find.bean.Gank;
import com.zack.gank.gankandroid.sexy.SexyChagneBgInterface;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Android on 2017/7/24.
 */

public class SexyDetailAdapter extends PagerAdapter {

    Context mContext;
    List<Gank> imageList;
    SexyChagneBgInterface sexyChagneBgInterface;
    public SexyDetailAdapter(Context mContext, ArrayList<Gank> list) {
        this.mContext=mContext;
        this.sexyChagneBgInterface= (SexyChagneBgInterface) mContext;
        this.imageList=list;
    }

    @Override
    public int getCount() {
        return imageList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(final ViewGroup container, int position) {

       final View view= LayoutInflater.from(mContext).inflate(R.layout.sexy_detail_vp,null);
        final ImageView ivVp= (ImageView) view.findViewById(R.id.iv_vp);
//        Glide.with(mContext).load(imageList.get(position).url).into(ivVp);
        Glide.with(mContext).load(imageList.get(position).url).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                ivVp.setImageBitmap(resource);
                Palette palette= Palette.generate(resource);
                Palette.Swatch swatch =palette.getLightVibrantSwatch();
                if(swatch!=null){
                    sexyChagneBgInterface.getPaletteColor(swatch.getRgb());

                }
            }}
            );
        container.addView(view);
        return view;
      /* ImageView imageView = new ImageView(mContext);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        imageView .setLayoutParams(layoutParams);
        ((ViewPager) container).addView(imageView);
        Glide.with(mContext)
                .load(imageList.get(position).url)
                .centerCrop()
                .crossFade()
                .into(imageView);
        return imageView;*/
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
       /* ImageView view = (ImageView) object;
        if (view == null)
            return;
        Glide.clear(view);     //核心，解决OOM
        ((ViewPager) container).removeView(view);*/

        View view = (View) object;
        if (view == null)
            return;
        Glide.clear(view.findViewById(R.id.iv_vp));     //核心，解决OOM
        ((ViewPager) container).removeView(view);

    }
}
